package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.ota.mo.CancelTrans;
import com.air.ota.mo.InboundMsg;
import com.air.ota.mo.OutboundMsg;
import com.air.ota.service.ICancelResultHandlerService;
import com.air.ota.service.ICancelTransService;
import com.air.ota.service.IOutboundMsgService;
import com.air.ota.service.IPkgSequenceService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.SZTInterface;
import com.air.ota.vo.SZTBaseResponseVO;
import com.air.ota.vo.SZTCancelInitRequestVO;
import com.air.ota.vo.SZTCancelResultReqTransVO;
import com.air.ota.vo.SZTCancelResultRequestVO;
import com.air.ota.vo.SZTCancelResultResTransVO;
import com.air.ota.vo.UTKCancelResultDownlinkVO;
import com.air.ota.vo.UTKCancelResultUplinkVO;

@Service("cancelResultHandlerService")
public class CancelResultHandlerServiceImp  extends BaseUplinkMsgHandler 
	implements	ICancelResultHandlerService {
	
	@Autowired
	ICancelTransService cancelTransService;
	@Autowired
	IOutboundMsgService outboundMsgService;
	@Autowired
	IPkgSequenceService pkgSequenceService;
	
	@Override
	public void process(UTKCancelResultUplinkVO pkgVO, InboundMsg inboundMsg)
			throws Exception {
		System.out.println("------------Ready to Process Cancel Result Request");
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("cancelSequence",HexStringUtil.bytesToHexString(pkgVO.getCancelSequence()) , QueryExpression.EQUALS);
		condition.addQueryCondition("cellphone",inboundMsg.getPhoneNumber() , QueryExpression.EQUALS);
		List<CancelTrans> queryResult= cancelTransService.queryByCondition(condition);
		CancelTrans trans = queryResult.get(0);
		
		if ((trans.getResult()<0) || (trans.getResult()>=AppConstant._cancelResult_result_success)){
			 System.out.println("DISCARD CANCEL RESULT MSG,Cause Result="+trans.getResult());
			 return;
		 }
		
		trans.setMac2(HexStringUtil.bytesToHexString(pkgVO.getMac2()));
		trans.setTac(HexStringUtil.bytesToHexString(pkgVO.getTac()));
		trans.setEwalletRemain(HexStringUtil.bytesToHexString(pkgVO.getEwalletRemain()));
		trans.setCardTransSequence(HexStringUtil.bytesToHexString(pkgVO.getCardTransSequence()));
		trans.setCardRandom(HexStringUtil.bytesToHexString(pkgVO.getCardRandom()));
		
		cancelTransService.updateById(trans);
		
		unlockRetryOutboundMsg(trans.getId(),AppConstant._outbound_msg_type_cancelInitResponse);
		
		//调用深圳通接口
		byte[] deviceSequence = HexStringUtil.hexStringToBytes(trans.getDeviceSequence());
		byte[] sztSequence = HexStringUtil.hexStringToBytes(trans.getSztId());
		byte[] result = HexStringUtil.hexStringToBytes("00000000");
		
		SZTCancelResultReqTransVO sztCancelResultReqTransVO = new SZTCancelResultReqTransVO(
					deviceSequence,sztSequence,trans.getDeviceTriggerTime(),
					HexStringUtil.hexStringToBytes(trans.getCardAppSequence()),
					result,
					HexStringUtil.hexStringToBytes(trans.getMac2()),
					HexStringUtil.hexStringToBytes(trans.getTac()),
					HexStringUtil.hexStringToBytes(trans.getEwalletRemain()),
					HexStringUtil.hexStringToBytes(trans.getCardTransSequence()),
					HexStringUtil.hexStringToBytes(trans.getCardRandom())
				);
		SZTCancelResultRequestVO requestVO = new SZTCancelResultRequestVO(sztCancelResultReqTransVO.getTransDataHexString()); 
		SZTInterface sztInterface = new SZTInterface();
		byte[] response=sztInterface.sendRequest(HexStringUtil.hexStringToBytes(requestVO.getMsgPackage()));
		SZTBaseResponseVO responseVO = new SZTBaseResponseVO();
		responseVO.parse(response);
		
		SZTCancelResultResTransVO sztCancelResultResTransVO = new SZTCancelResultResTransVO();
		sztCancelResultResTransVO.parse(HexStringUtil.hexStringToBytes(responseVO.getData()));
		
		//更新CancelTrans对象
		if (sztCancelResultResTransVO.getTransResult()==0x00){//Success
			System.out.println("success");
			trans.setMac2Result(HexStringUtil.byteToHexString(sztCancelResultResTransVO.getMac2Result()));
			trans.setOuterAuthCode(HexStringUtil.bytesToHexString(sztCancelResultResTransVO.getOuterAuthCode()));
			trans.setResult(AppConstant._cancelResult_result_success);
		}else{
			trans.setResult(AppConstant._cancelResult_result_failure);
			System.out.println("ERROR CODE="+HexStringUtil.byteToHexString(sztCancelResultResTransVO.getTransResult()));
		}
		this.cancelTransService.updateById(trans);
		//Build UTK CancelResult Response Pkg
		UTKCancelResultDownlinkVO cancelResultDownLinkVO = new UTKCancelResultDownlinkVO(
				pkgVO.getCardSequence(),pkgSequenceService.getNextSequence(trans.getCellphone()),
				HexStringUtil.hexStringToBytes(trans.getCardAppSequence()),
				HexStringUtil.hexStringToBytes(trans.getCancelSequence()),
				HexStringUtil.hexStringToByte(trans.getMac2Result()),
				HexStringUtil.hexStringToBytes(trans.getOuterAuthCode())
				);
		byte[] responsePkg=cancelResultDownLinkVO.buildPkg();
		
		OutboundMsg outboundMsg = new OutboundMsg();
		outboundMsg.setContent(HexStringUtil.bytesToHexString(responsePkg));
		outboundMsg.setDest(0);
		outboundMsg.setPhoneNumber(trans.getCellphone());
		outboundMsg.setSendDate(System.currentTimeMillis());
		outboundMsg.setTransId(trans.getId());
		outboundMsg.setNeedRetry(1);
		outboundMsg.setMsgType(AppConstant._outbound_msg_type_cancelResultResponse);
		outboundMsgService.insert(outboundMsg);
		
	}
	
}
